package com.ccrego.haiyi.account.login;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ccrego.haiyi.R;
import com.ccrego.haiyi.account.ParamKey;
import com.ccrego.haiyi.account.UrlCenter;
import com.ccrego.haiyi.utils.FragmentUtils;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.framework.httprequest.HttpRequest;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.frameworkbase.customviews.edittext.PasswordEditText;
import com.pachong.android.frameworkbase.utils.EasyToast;
import com.pachong.android.frameworkbase.utils.MD5Util;
import com.pachong.android.frameworkbase.utils.RegularUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.OnClick;

public class UpdataActivity extends BaseActivity {
    String phone;
    String code;
    @Bind(R.id.itetUpdataPassword)
    PasswordEditText mItetUpdataPassword;
    @Bind(R.id.itetUpdataPassword_again)
    PasswordEditText mItetUpdataPasswordAgain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        phone = getIntent().getStringExtra("phone");
        code = getIntent().getStringExtra("code");
        initToolsBar();
    }

    private void initToolsBar() {
        setTitle(R.string.updata);
        getCustomToolbar().addLeftImageButton(R.mipmap.icon_back1, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    private String getPassWord() {
        return mItetUpdataPassword.getEditText().toString().trim();
    }

    private String getComfirmPassWord() {
        return mItetUpdataPasswordAgain.getEditText().toString().trim();
    }

    public static void start(Activity activity, String phone, String code) {
        Intent intent = new Intent(activity, UpdataActivity.class);
        intent.putExtra("phone", phone);
        intent.putExtra("code", code);
        activity.startActivity(intent);
    }


    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_updata, null);
    }

    @OnClick(R.id.btn_updata_next)
    public void onViewClicked() {
        //判断两次密码是否一致
        checkPassWord();
    }

    private void checkPassWord() {
        if (TextUtils.isEmpty(getPassWord())) {
            FragmentUtils.showWarnDialog("密码不能为空", getSupportFragmentManager());
            return;
        } else if (!RegularUtils.isPassword(getPassWord())) {
            FragmentUtils.showWarnDialog("密码最多为6-25个字符", getSupportFragmentManager());
            return;
        }
        if (getPassWord().equals(getComfirmPassWord()) == true) {
            updataPassWord();
        } else {
            FragmentUtils.showWarnDialog("两次密码输入不相同", getSupportFragmentManager());
        }


    }

    //修改密码
    private void updataPassWord() {
        HttpRequest request = new HttpRequest(UpdataActivity.this);
        JSONObject jobj = new JSONObject();
        final String enCodePwd = MD5Util.getMd5(getPassWord());
        try {
            jobj.put(ParamKey.PHONE, phone);
            jobj.put(ParamKey.PASSWORD, enCodePwd);
            jobj.put(ParamKey.CAPTCHA, code);
            request.put(UrlCenter.UPDATA_PASSWORD, jobj, new DataRequestListener<Object>(Object.class) {
                @Override
                public void onStart(Context context, String url) {
                    super.onStart(context, url);
                }

                @Override
                public void onSuccess(Object o) {
                    super.onSuccess(o);
                    EasyToast.showToast(getApplicationContext(), "修改成功");
                    LoginActivity.start(UpdataActivity.this);
                }

                @Override
                public void onFailure(Throwable t, int errorNo, String strMsg) {
                    super.onFailure(t, errorNo, strMsg);
                    EasyToast.showToast(getApplicationContext(), strMsg);
                }
            });


        } catch (JSONException e) {
            e.printStackTrace();
        }


    }
}
